import com.jfinal.plugin.activerecord.ActiveRecordException;
import gl.java.javascript.ScriptManger;
import gl.pk.db.DBUtil;
import gl.pk.db.model.HumBag;
import gl.pk.db.model.HumProperty;
import jdk.nashorn.internal.runtime.Scope;
import lombok.extern.slf4j.Slf4j;

import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptException;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Slf4j
public class Main {

    public static void main(String[] args) throws IOException, ScriptException {
        //启动数据库模块
        DBUtil.init();

        //启动网络模块
//        RoomService.main(null);

        //启动脚本模块
        ScriptManger scriptManger = new ScriptManger();
        //加载脚本文件
        scriptManger.setup("D:\\Users\\Administrator\\Documents\\PK\\server\\PKServer\\script\\");

        HumProperty hum = HumProperty.dao.findById(1);
        //触发脚本函数
//        scriptManger.invokeFunction("onUserLogin",hum);
//        log.info("hum {}",hum);
        scriptManger.getEngine().getContext().setAttribute("$p0",hum, ScriptContext.ENGINE_SCOPE);
        scriptManger.invokeFunction("npc_call","新手接待员.checkLevel",hum);
//        testDB();
    }

    private static void testDB() {
        HumProperty humProperty = new HumProperty();
        humProperty.setIndex(1);
        try {
            humProperty.setName("葛大帅");
            humProperty.setAlias("admin");
            humProperty.setAccount("rrrr");
            humProperty.save();
        } catch (ActiveRecordException e) {
            if (e.getMessage().indexOf("Duplicate entry") > 0) {
                log.warn("[WARN] insert a new user is fail. user.name is existed ");
            }
        }
        List<HumBag> humBagList = HumBag.dao.find("select * from hum_bag where hum_index = ?", humProperty.getIndex());
        for (HumBag item : humBagList) {
            item.setItemDuraCurrent(item.getItemDuraCurrent() + 1);
            item.update();
            log.info("the owner of '{}'(position:{}) is {} ", item.getItemName(), item.getPostion(), humProperty.getName());
        }
    }
}
